<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>7.组合模式</title>
</head>
<body>
组合模式将对象组合成树形结构，以表示"部分-整体"的层次结构
1.表示树形结构
2.利用对象多态性统一对待组合对象和单个对象

<button id="button">遥控器</button>
</body>
<script type="text/javascript">
	var MacroCommand = function(){
		return {
			commandsList : [],
			add : function( command ){
				this.commandsList.push( command );
			},
			execute : function(){
				for(var i = 0,command;command = this.commandsList[i++];){
					command.execute();
				}
			}
		}
	}


	var openAcCommand = {
		execute: function(){
			console.log('打开空调');
		}
	}

	/* 家里的电视和音响是连接在一起的 */
	var openTvCommand = {
		execute : function(){
			console.log('打开电视');
		}
	}

	var openSoundCommand = {
		execute : function(){
			console.log('打开音响');
		}
	}

	var macroCommand1 = MacroCommand();
	macroCommand1.add( openTvCommand );
	macroCommand1.add( openSoundCommand );

	/* 关门，打开电脑和登录qq */
	var closeDoorCommand = {
		execute : function(){
			console.log('关门');
		}
	}

	var openPcCommand = {
		execute : function(){
			console.log('开电脑');
		}
	}

	var openQQCommand = {
		execute : function(){
			console.log('登录qq');
		}
	}

	var macroCommand2 = MacroCommand();
	macroCommand1.add( closeDoorCommand );
	macroCommand1.add( openPcCommand );
	macroCommand1.add( openQQCommand );

	/* 把所有的命令组合成一个超级命令 */
	var macroCommand = MacroCommand();
	macroCommand.add( openAcCommand );
	macroCommand.add( macroCommand1 );
	macroCommand.add( macroCommand2 );

	/* 最后给遥控器绑定"超级命令" */
	var setCommand = (function(command){
		document.getElementById('button').onclick = function(){
			command.execute();
		}
	})( macroCommand );





	/* 扫描文件夹 */
	var Folder = function(name){
		this.name = name;
		this.parent = null;
		this.files = [];
	};

	Folder.prototype.add = function(file) {
		file.parent = this;
		this.files.push(file);
	};

	Folder.prototype.scan = function() {
		console.log( "开始扫描文件夹" + this.name );
		for(var i = 0 , file, files = this.files;file = files[i++]; ){
			file.scan();
		}
	};
	Folder.prototype.remove = function() {
		if(!this.parent){
			return;
		}
		for(var files = this.parent.files, l = files.length - 1; l>=0; l--){
			var file = files[l];
			if(file === this){
				files.splice(l , 1);
			}
		}
	};

	var File = function(name){
		this.parent = null;
		this.name = name;
	}

	File.prototype.add = function() {
		throw new Error('文件下面不能再添加文件');
	};
	File.prototype.scan = function() {
		console.log( '开始扫描文件' + this.name );
	};
	File.prototype.remove = function() {
		if(!this.parent){
			return;
		}
		for(var files = this.parent.files, l = files.length - 1; l>=0; l--){
			var file = files[l];
			if(file === this){
				files.splice(l , 1);
			}
		}
	};

	var folder = new Folder('学习资料');
	var folder1 = new Folder('Javascript');
	var folder2 = new Folder('jQuery');

	var file1 = new File('javascript设计模式与开发实践');
	var file2 = new File('精通jQuery');
	var file3 = new File('重构与模式');

	folder1.add(file1);
	folder2.add(file2);

	folder.add( folder1 );
	folder.add( folder2 );
	folder.add( file3 );

	var folder3 = new Folder('nodeJs');
	var file4 = new File('深入浅出Node.js');
	folder3.add( file4 );

	var file5 = new File('javascript语言精髓与编程实践');

	folder.add(folder3);
	folder.add(file5);
	// file5.remove();
	// folder3.remove();

	folder.scan();


</script>
</html>